Chapter 15: Structural Equation Modelling
Welcome to the online content for Chapter 15!
As always, I’ll assume that you’ve already read up to this chapter of the book and worked through the online content for the previous chapters. If not, please do that first.
As always, click the ‘Run Code’ buttons below to execute the R code. Remember to wait until they say ‘Run Code’ before you press them. And be careful to run these boxes in order if later boxes depend on you having done other things previously.
Looking at the data
First, we’ll download the data:
We can see that we have three variables. Each row is a different person, so there are 20 people in total.
Let’s see what the correlations look like:
The values match those discussed in the chapter.
Mediation Analysis
Running a median analysis in R requires us to install the psych package, but this doesn’t yet work on WebR. So, to use the code below, you’ll need to download and install R on your own computer, as described in the Introduction.
When you’ve done that, the code below will enable you to run a mediation analysis on these data, by using the mediate
function.
Lines that begin with a # symbol are just comments, to explain what’s going on, and don’t actually do anything.
#Install the psych package
install.packages("psych")
#Load the library
library(psych)
#Read in the data from the chapter
<- read.csv("https://colinfoster77.github.io/Stats/Chap15LocationHeightDiet.csv")
people
people
#It's the same data as used above.
#Now we use the 'mediate' function in the psych package to do the mediation analysis. We put the 'diet' variable in brackets to show that it's the variable that we want as the mediator.
<- mediate(height ~ location + (diet), data=people)
results summary(results)
#The path diagram and the values match those discussed in the chapter.